import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager

#总是报错
# #打开浏览器 ---驱动管理
# ChromeIns = ChromeDriverManager().install()
# #创建谷歌浏览器驱动对象
# driver = webdriver.Chrome(service = Service(ChromeIns))

#核心代码
#下载对应版本的驱动来解决问题
chrome_driver_path = 'G:\浏览器下载\chromedriver-win64\chromedriver-win64\chromedriver.exe'
# 设置 ChromeDriver
# service = Service(executable_path=chrome_driver_path)
#Selenium 4之后，executable_path参数在Service类中被弃用

#创建 Service 对象指向 ChromeDriver 可执行文件   作用：管理 ChromeDriver 的启动和停止
service = Service(chrome_driver_path)
#创建 Chrome 浏览器选项对象
options = Options()
options.add_argument("--disable-blink-features=AutomationControlled")  #作用：禁用 Blink 引擎的自动化控制检测，用于隐藏自动化特征，防止网站识别 Selenium
options.add_experimental_option("excludeSwitches", ["enable-automation"]) #作用：排除 "enable-automation" 开关  会隐藏浏览器顶部的 "正受到自动测试软件控制" 提示
options.add_experimental_option('useAutomationExtension', False) #作用: 禁用自动化扩展  进一步防止被检测为自动化工具
# options.add_argument("-headless")  #无头模式
# options.page_load_strategy = 'eager' # 加载策略： DOM访问已准备就绪，但是诸如图像的其他资源可能仍在下载
# options.page_load_strategy = 'normal' # 加载策略： 默认值，等待所有资源下载
# options.page_load_strategy = 'none' # 加载策略： 完全变化阻塞WebDriver


driver = webdriver.Chrome(service=service, options=options)

driver.get("https://www.baidu.com")
print(driver.title)

#警告弹窗
# #输入网址
# driver.get("file:///G:/Test_Html/assert.html")
# time.sleep(2)
#
# #点击调起弹窗
# driver.find_element(By.CSS_SELECTOR, "body > button").click()
#
# #切换到弹窗
# alert = driver.switch_to.alert
# time.sleep(2)
# #确定
# alert.accept()
# # #取消
# # alert.dismiss()
# time.sleep(2)
#
# #弹窗关闭之后才能执行下面的代码
# driver.find_element(By.CSS_SELECTOR, "body > button").click()


# #确认弹窗
# driver.get("file:///G:/Test_Html/confirm.html")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "body > button").click()
#
# alert = driver.switch_to.alert
# time.sleep(2)
# ##确认
# #alert.accept()
# #取消
# alert.dismiss()
#
# time.sleep(2)


# #提示弹窗
# driver.get("file:///G:/Test_Html/prompt.html")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "body > button").click()
# #输入文本 + 确定 + 取消
# alert = driver.switch_to.alert
# time.sleep(1)
# alert.send_keys("自动化脚本提示弹窗")
# alert.accept()
# time.sleep(1)
# #alert.dismiss()



# driver.get("https://www.baidu.com")
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("薛之谦")
# driver.find_element(By.CSS_SELECTOR, "#su").click()
#显示等待
# time.sleep(3)
# #点击页面中的百度百科
# driver.find_element(By.CSS_SELECTOR, "#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")


# #隐式等待
# driver.implicitly_wait(3)
# driver.get("https://www.baidu.com")
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("薛之谦")
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# #点击页面中的百度百科
# driver.find_element(By.CSS_SELECTOR, "#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")



# #显示等待
# driver.implicitly_wait(3)
# driver.get("https://www.baidu.com")
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("薛之谦")
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# #点击页面中的百度百科
# #添加显示等待
# #创建显示等待类对象
# wait = WebDriverWait(driver, 3)
#wait.until(expected_conditions)
#wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(1) > span")))

# driver.get("https://www.baidu.com")
# #隐式等待设置为10s，显⽰等待设置为15s，那么结果会是10+15=25s吗？
# driver.implicitly_wait(10)  #隐式等待
# wait = WebDriverWait(driver,15) #显示等待
# start = time.time() #保存到当前时间 (时间戳)
# try:
#  res = wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="hotsearchaaaaaa-content-wrapper"]/li[1]/a/span[2]')))
# except:
#  end = time.time()
#  print("no such element")
# driver.quit()
# print(end-start)



# driver.get("https://www.baidu.com")
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("薛之谦")
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# time.sleep(2)
# #后退：回到百度首页
# driver.back()
# time.sleep(2)
# #前进：再回到刚刚搜索的页面
# driver.forward()
# time.sleep(2)
# #刷新
# driver.refresh()
# time.sleep(2)

# driver.get("file:///G:/Test_Html/Upload.html")
# driver.find_element(By.CSS_SELECTOR, "#uploadBtn").send_keys("C:\\Users\\tangt\\Desktop\\test.txt")
# time.sleep(3)


# # 打开本地HTML文件
# driver.get("file:///G:/Test_Html/Upload.html")
#
# # 定位到隐藏的input元素
# file_input = driver.find_element(By.ID, "fileInput")  # 使用ID定位
#
# #  发送文件路径到input元素
# file_path = r"C:\Users\tangt\Desktop\test.txt"
# file_input.send_keys(file_path)
#
# # 验证状态变更（可增加显式等待）
# time.sleep(0.5)  # 等待DOM更新
# status_element = driver.find_element(By.ID, "fileStatus")
# print("当前状态:", status_element.text)  # 应该显示文件名

# time.sleep(3)

driver.quit()
